﻿namespace IPMS.BLL
{
    using IPMS.DAL;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.CompilerServices;

    public class ProjectDocumentUploadLog
    {
        private string _BatchID;
        private string _Remark;
        private string _Source_TransferFileFullPath;
        private string _Target_TransferFileFullPath;
        private DateTime _TransDate;

        public ProjectDocumentUploadLog()
        {
        }

        public ProjectDocumentUploadLog(string BatchID, DateTime TransDate, string Remark, string Source_TransferFileFullPath, string Target_TransferFileFullPath)
        {
            this._BatchID = BatchID;
            this._Remark = Remark;
            this._TransDate = TransDate;
            this._Target_TransferFileFullPath = Target_TransferFileFullPath;
            this._Source_TransferFileFullPath = Source_TransferFileFullPath;
        }

        public List<ProjectDocumentUploadLog> GetSearchResult(string StartingBatchID, string EndingBatchID, DateTime StartingTransDate, DateTime EndingTransDate, string Remark)
        {
            SqlCommand sqlCmd = new SqlCommand();
            DataAccessLayer DAL = new DataAccessLayer();
            string cmdText = "GetProjectDocumentUploadSearchSp";
            DAL.SetCommandType(sqlCmd, CommandType.StoredProcedure, cmdText);
            DAL.AddParamToSQLCmd(sqlCmd, "@StartingBatchID", SqlDbType.NVarChar, 30, ParameterDirection.Input, StartingBatchID);
            DAL.AddParamToSQLCmd(sqlCmd, "@EndingBatchID", SqlDbType.NVarChar, 30, ParameterDirection.Input, EndingBatchID);
            DAL.AddParamToSQLCmd(sqlCmd, "@StartingTransDate", SqlDbType.DateTime, 8, ParameterDirection.Input, (((string.Format("{0:MM/dd/yyyy}", StartingTransDate)) == "01/01/0001" || (string.Format("{0:MM/dd/yyyy}", StartingTransDate)) == "01-01-0001") ? "1/1/1753" : string.Format("{0:MM/dd/yyyy}", StartingTransDate)));
            DAL.AddParamToSQLCmd(sqlCmd, "@EndingTransDate", SqlDbType.DateTime, 8, ParameterDirection.Input, (((string.Format("{0:MM/dd/yyyy}", EndingTransDate)) == "01/01/0001" || (string.Format("{0:MM/dd/yyyy}", StartingTransDate)) == "01-01-0001") ? "12/31/9999" : string.Format("{0:MM/dd/yyyy}", EndingTransDate)));
            DAL.AddParamToSQLCmd(sqlCmd, "@Remark", SqlDbType.NVarChar, 100, ParameterDirection.Input, Remark);
            List<ProjectDocumentUploadLog> searchResult = new List<ProjectDocumentUploadLog>();
            this.TExecuteReaderCmd<ProjectDocumentUploadLog>(sqlCmd, new TGenerateListFromReader<ProjectDocumentUploadLog>(this.TGenerateSearchListFromReader<ProjectDocumentUploadLog>), ref searchResult);
            return searchResult;
        }

        private void TExecuteReaderCmd<T>(SqlCommand sqlCmd, TGenerateListFromReader<T> gcfr, ref List<T> List)
        {
            if (this.ConnectionString == string.Empty)
            {
                throw new ArgumentOutOfRangeException("ConnectionString");
            }
            if (sqlCmd == null)
            {
                throw new ArgumentNullException("sqlCmd");
            }
            using (SqlConnection cn = new SqlConnection(this.ConnectionString))
            {
                sqlCmd.Connection = cn;
                cn.Open();
                gcfr(sqlCmd.ExecuteReader(), ref List);
            }
        }

        private void TGenerateSearchListFromReader<T>(SqlDataReader returnData, ref List<ProjectDocumentUploadLog> searchResult)
        {
            while (returnData.Read())
            {
                ProjectDocumentUploadLog pdLog = new ProjectDocumentUploadLog((string) returnData["BatchID"], (DateTime) returnData["TransDate"], (string) returnData["Remark"], (string) returnData["Source_TransferFileFullPath"], (string) returnData["Target_TransferFileFullPath"]);
                searchResult.Add(pdLog);
            }
        }

        public string BatchID
        {
            get
            {
                return this._BatchID;
            }
        }

        protected string ConnectionString
        {
            get
            {
                if (ConfigurationManager.ConnectionStrings["LocalSqlServer"] == null)
                {
                    throw new NullReferenceException("ConnectionString configuration is invalid.");
                }
                string connectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
                if (string.IsNullOrEmpty(connectionString))
                {
                    throw new NullReferenceException("ConnectionString configuration is invalid.");
                }
                return connectionString;
            }
        }

        public string Remark
        {
            get
            {
                return this._Remark;
            }
        }

        public string Source_TransferFileFullPath
        {
            get
            {
                return this._Source_TransferFileFullPath;
            }
        }

        public string Target_TransferFileFullPath
        {
            get
            {
                return this._Target_TransferFileFullPath;
            }
        }

        public DateTime TransDate
        {
            get
            {
                return this._TransDate;
            }
        }

        private delegate void TGenerateListFromReader<T>(SqlDataReader returnData, ref List<T> tempList);
    }
}

